﻿using commonImportPlugin.Common;
using commonImportPlugin.Config;
using commonImportPlugin.Factorys;
using ICMS.PluginInterfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace commonImportPlugin.Decrypt
{
    public class DecryptCoreZGNYYH : DecryptCoreF
    {
        DecryptZGNYYHbase decryptBase = null;//基础函数类
        Context context = null;//解密配置
        CommonUtil commonUtil = CommonUtil.GetInstance();//通用工具类 
        public DecryptCoreZGNYYH(Context _context)
        {
            if (_context == null)
                throw new Exception("加载加机密配置类失败！");
            context = _context;
            decryptBase = new DecryptZGNYYHbase(context);
        }
        /// <summary>
        /// 解密函数
        /// </summary>
        /// <param name="decryptDatas"></param>
        /// <param name="succeedCounts"></param>
        /// <returns></returns>
        public override List<ICData> DecryptCore(List<ICData> decryptDatas, out int succeedCounts)
        {
            try
            {
                List<string> failDatas = new List<string>();
                failDatas.Add("");//预留一个位置写备注信息
                List<ICData> succeedDecryptDatas = new List<ICData>();//解密成功的数据
                bool isSucceedTrack2=false;
                bool isSucceedTrack3=false;

                string[] seperators = new string[] { "%", "?;", "?_;", "?" };
                int dataCounts = decryptDatas.Count;//最初的数据量
                //打开加密机
                decryptBase.hsmConnect();
                              
                for (int i = 0; i < decryptDatas.Count; i++)
			    {
                    #region ===解密2,3磁道数据===
                    if (string.IsNullOrEmpty(decryptDatas[i].Track1))
                    {
                        failDatas.Add(string.Format("银行卡号：{0};磁条数据为空",decryptDatas[i].BankCardID));
                        continue;
                    }

                    /////////获取原始数据//////
                    string[] tracks = decryptDatas[i].Track1.Split(seperators, StringSplitOptions.None);
                    string originTrack2 = tracks[2].Trim();
                    string originTrack3 = tracks[3].Trim();

                    /////////解密操作//////////
                    string track2Data = "";
                    string track3Data = "";
                    isSucceedTrack2 = decryptBase.GetTrackData(originTrack2, out track2Data);
                    if (isSucceedTrack2)
                    {
                        isSucceedTrack3 = decryptBase.GetTrackData(originTrack3, out track3Data);
                    }                  

                    if (!isSucceedTrack2 || !isSucceedTrack3)
                    {
                        for (int j = i; j < decryptDatas.Count; j++)
                        {
                           failDatas.Add(string.Format("银行卡号：{0};二三磁解密失败",decryptDatas[j].BankCardID));//匹配失败的
                        }                                               
                        break;
                    }

                    ////////拼成指定格式//////
                    //截取2磁道
                    track2Data = commonUtil.SubString(track2Data,2,-2);//除掉头和尾                    
                    //截取3磁道
                    track3Data = commonUtil.SubString(track3Data, 2, -2);//除掉头和尾

                    string trackData = string.Format("%{0}?;{1}?_;{2}?", "", track2Data, track3Data);//拼成指定格式 
                    //decryptBase.checkTrackDatas(trackData);
                    decryptDatas[i].Track1 = trackData;
                    //收集解密成功的数据
                    succeedDecryptDatas.Add(decryptDatas[i]);
                    #endregion ===
                }
                //有存在解密失败的数据
                if (failDatas.Count > 1)
                {
                    //处理是否继续
                    decryptBase.IsContinueDeal(dataCounts, context.taskId, succeedDecryptDatas.Count, failDatas);       
                }
                succeedCounts = succeedDecryptDatas.Count;
                return succeedDecryptDatas;
            }
            catch (Exception e)
            {               
                throw new Exception(e.Message);
            }
            finally
            {
                //关闭加密机
                decryptBase.hsmClose();
            }          
        }
             
    }
    /// <summary>
    /// 通用数据解密执行类
    /// </summary>
    public class CommonDecryptClass
    {
        public List<ICData> commonDecrypt(List<ICData> icData, string decryptMode,Context context)
        {
            List<ICData> afterDecryptDatas = new List<ICData>();
            #region 加密方式
            switch (decryptMode)
            {
                case "NYYH23Track"://农业银行
                    int succeedCounts = 0;
                    setDecryptCoreFactory setDecryptCoreFactory = new setDecryptCoreFactory();
                    DecryptCoreF decryptCore = setDecryptCoreFactory.CreateFactory<Context>("NYYH23Track", context);
                    afterDecryptDatas = decryptCore.DecryptCore(icData, out succeedCounts);
                    break;
                default:
                    return null;
            }
            return afterDecryptDatas;
            #endregion
        }
    }
}
